package com.click369.mianshiqingbao.controller;

import com.click369.mianshiqingbao.pojo.UserBean;
import com.click369.mianshiqingbao.pojo.entity.PageResult;
import com.click369.mianshiqingbao.pojo.entity.Result;
import com.click369.mianshiqingbao.service.UserService;
import com.click369.mianshiqingbao.util.MianShiQingBaoHelper;
import com.click369.mianshiqingbao.util.StaticFinalFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户信息的控制器类
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 条件分页查询用户信息列表
     * @param userbean 用户信息对象
     * @param page 页码
     * @param size 每页记录数
     * @return 分页查询信息的封装对象
     */
    @RequestMapping("/search.do")
    @ResponseBody
    public PageResult search(@RequestBody UserBean userbean, int page, int size){
        return userService.findPage(userbean,page, size);
    }

    /**
     * 新增用户信息
     * @param //userbean 用户信息对象
     * @return 成功或失败的提示信息封装类对象
     */
    @RequestMapping(value = "/add.do")
    @ResponseBody
    public Result add(@RequestBody UserBean userbean,String smscode){
        System.out.println("telNumber=="+userbean.getTelNumber());
        System.out.println("smscode=="+smscode);
        //判断手机号码
        if (!MianShiQingBaoHelper.panDuanPhoneNum(userbean.getTelNumber())) {
            return new Result(false, StaticFinalFile.ISNOTPHONENUM);
        }
        //判断手机号码是否被使用过
        if(!userService.phoneNumExist(userbean.getTelNumber())){
            return new Result(false, StaticFinalFile.FAILURE);
        }
        //判断密码
        if (userbean.getUserPass().equals("")||userbean.getUserPass().length()<=0) {
            return new Result(false, StaticFinalFile.FAILURE);
        }
        //判断短信验证码
        if (smscode.equals("")||smscode.length()<=0) {
            return new Result(false, StaticFinalFile.FAILURE);
        }
        String code=(String)redisTemplate.boundValueOps(userbean.getTelNumber()).get();
        if (!smscode.equals(code)) {
            redisTemplate.delete(userbean.getTelNumber());
            return new Result(false, StaticFinalFile.FAILURE);
        }
        redisTemplate.delete(userbean.getTelNumber());
        try {
            userbean.setTelNumber(userbean.getTelNumber());
            userbean.setUserPass(userbean.getUserPass());
            userService.add(userbean);
            return new Result(true, StaticFinalFile.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StaticFinalFile.FAILURE);
        }
    }

    /**
     * 根据id查询用户信息对象
     * @param id 用户ID
     * @return 用户信息对象
     */
    @RequestMapping("/findOne.do")
    @ResponseBody
    public UserBean findOne(Long id){
        return userService.findOne(id);
    }

    /**
     * 修改用户信息
     * @param userbean 用户信息对象
     * @return 成功或失败的提示信息封装类对象
     */

    @RequestMapping("/update.do")
    @ResponseBody
    public Result update(@RequestBody UserBean userbean){
        try {
            //判断用户昵称是否被使用过
            if(!userService.userNickExist(userbean.getUserNick())){
                return new Result(false, StaticFinalFile.USERNICKEXIST);
            }
            userService.update(userbean);
            return new Result(true,StaticFinalFile.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StaticFinalFile.FAILURE);
        }
    }

    /**
     * 修改用户信息(ceshi)
     * @return 成功或失败的提示信息封装类对象
     */
    // ceshidizhi
    //http://localhost:8080/user/update.do?userId=4&userNick=wangwu
    // &isGender=0&userMail=123@123.com&userJobid=3&userEducation=3&userTech=xuexi
    /*
    @RequestMapping("/update.do")
    @ResponseBody
    public Result update(HttpServletRequest req){
        try {
            Long userId = Long.parseLong(req.getParameter("userId"));
            UserBean userbean = userService.findOne(userId);
            String userNick = req.getParameter("userNick");
    //      Integer isGender = Integer.parseInt(req.getParameter("isGender"));
            boolean isGender = true;
            if(req.getParameter("isGender").equals("1")){
                isGender = true;
            }else{
                isGender = false;
            }
            String userMail = req.getParameter("userMail");
            Integer userJobid = Integer.parseInt(req.getParameter("userJobid"));
            String userEducation = req.getParameter("userEducation");
            String userTech = req.getParameter("userTech");
            //判断用户昵称是否被使用过
            if(!userService.userNickExist(userNick)){
                return new Result(false, StaticFinalFile.USERNICKEXIST);
            }
            userbean.setUserNick(userNick);
            userbean.setIsGender(isGender);
            userbean.setUserMail(userMail);
            userbean.setUserJobid(userJobid);
            userbean.setUserEducation(userEducation);
            userbean.setUserTech(userTech);
            userService.update(userbean);
                return new Result(true,StaticFinalFile.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StaticFinalFile.FAILURE);
        }
    }
*/
    /**
     * 批量删除用户信息
     * @param ids 包含有被删除用户信息的ID数组
     * @return 成功或失败的提示信息封装类对象
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public Result delete(Long ids[]){
        try {
            userService.delete(ids);
            return new Result(true,StaticFinalFile.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StaticFinalFile.FAILURE);
        }
    }
}
